const { got, URL } = require("./include");

//测试url
//http://localhost:8080/detail?id=6516
module.exports = function() {
  return async function(ctx, next) {
    // 允许来自所有域名请求
    ctx.set("Access-Control-Allow-Origin", "*");
    // 这样就能只允许 http://localhost:8080 这个域名的请求了
    // ctx.set("Access-Control-Allow-Origin", "http://localhost:8080");

    // 设置所允许的HTTP请求方法
    ctx.set("Access-Control-Allow-Methods", "OPTIONS, GET, PUT, POST, DELETE");

    // 字段是必需的。它也是一个逗号分隔的字符串，表明服务器支持的所有头信息字段.
    ctx.set(
      "Access-Control-Allow-Headers",
      "x-requested-with, accept, origin, content-type"
    );
    //取得get参数
    let request = ctx.request;
    let req_query = request.query;
    let req_querystring = request.querystring;
    // console.log(req_query);

    const response = await got(
      new URL(
        `https://way.jd.com/jisuapi/detail?id=${
          req_query.id
        }&appkey=87fcd8aa063a1c48934555687d5005bc`
      )
    );
    ctx.body = response.body;
  };
};
